library(SDMTools)

work.dir = "H:/Barra Work Directory/current.esoclim/"; setwd(work.dir)


cols = c('skyblue', 'slateblue', 'forestgreen', 'yellow', 'red')
months=c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')

zlim=NULL
for (ii in 1:12) { cat(ii,'\n')
      tasc = read.asc.gz(paste("tasmax", sprintf('%02i',ii), ".asc.gz",sep=''))
      zlim=range(c(zlim,as.vector(tasc)),na.rm=TRUE)
}
zlim=c(0,40) #given what you know about zlim

png(paste('tasmin_bins.png',sep=''), width=28, height=19, units='cm', res=300, pointsize=5, bg='white')


#make 4 columns of 3 rows of images 
par(mfrow=c(3,4),mar=c(0,1,0,1), oma=c(0,3,3,0))


#loop
for (ii in 1:12) {  cat(ii,'\n')
      tasc = read.asc.gz(paste("tasmin", sprintf('%02i',ii), ".asc.gz",sep=''))
      t40= which(tasc>=40)
      t35= which(tasc<40 & tasc>=35)
      t30= which(tasc<35 & tasc>=30)
      t20= which(tasc<30 & tasc>=20)
      t0= which(tasc<20)

      #overwrite the values in the asci

      tasc[t40]=4
      tasc[t35]=3
      tasc[t30]=2
      tasc[t20]=1
      tasc[t0]=0 
      
      image(tasc, ann=FALSE,axes=FALSE,col=cols, zlim=c(0,4))
      text (130, -40, months[ii], cex=4)
      if (ii==1) {legend(115,-8, c('-6','20+','30+','35+','40+'), fill=cols, title='Minimum Tepmerature', cex=3)}

      }

dev.off()
